/**create by liuhua at 2020年5月18日 下午6:05:22**/
package com.star.aries.service;

import java.text.SimpleDateFormat;
import java.util.List;

import org.springframework.stereotype.Service;

import com.fasterxml.jackson.core.JsonParser.Feature;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import lombok.SneakyThrows;

@Service
public class HnJson {
  private ObjectMapper objectMapper;

  @SuppressWarnings("deprecation")
  public HnJson() {
    this.objectMapper = new ObjectMapper();
    objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S"));
    objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    objectMapper.configure(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES, false);
    objectMapper.configure(DeserializationFeature.FAIL_ON_INVALID_SUBTYPE, false);
    objectMapper.configure(DeserializationFeature.FAIL_ON_MISSING_CREATOR_PROPERTIES, false);
    objectMapper.configure(DeserializationFeature.FAIL_ON_MISSING_EXTERNAL_TYPE_ID_PROPERTY, false);
    objectMapper.configure(DeserializationFeature.FAIL_ON_NULL_CREATOR_PROPERTIES, false);
    objectMapper.configure(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES, false);
    objectMapper.configure(DeserializationFeature.FAIL_ON_NUMBERS_FOR_ENUMS, false);
    objectMapper.configure(DeserializationFeature.FAIL_ON_READING_DUP_TREE_KEY, false);
    objectMapper.configure(DeserializationFeature.FAIL_ON_TRAILING_TOKENS, false);
    objectMapper.configure(DeserializationFeature.FAIL_ON_UNRESOLVED_OBJECT_IDS, false);
    objectMapper.configure(Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
  }

  /**
   * 将对象转换为json字符串
   */
  @SneakyThrows
  public <T> String obj2string(T t) {
    return objectMapper.writeValueAsString(t);
  }

  /**
   * description: 注意此修改配置，会影响此线程的objectMapper
   */
  @SneakyThrows
  public <T> String obj2string(T t, Class<?> serializationView) {
    return objectMapper.writerWithView(serializationView).writeValueAsString(t);
  }

  /**
   * 将字符串转list对象
   */
  @SneakyThrows
  public <T> List<T> str2list(String jsonStr, Class<T> cls) {
    JavaType t = objectMapper.getTypeFactory().constructParametricType(
        List.class, cls);
    return objectMapper.readValue(jsonStr, t);
  }

  @SneakyThrows
  public <T> T str2obj(String jsonStr, TypeReference<T> typeReference) {
    return objectMapper.readValue(jsonStr, typeReference);
  }


  /**
   * 将字符串转为对象
   */
  @SneakyThrows
  public <T> T str2obj(String jsonStr, Class<T> cls) {
    return objectMapper.readValue(jsonStr, cls);
  }


  /**
   * 将字符串转为json节点
   */
  @SneakyThrows
  public JsonNode str2node(String jsonStr) {
    return objectMapper.readTree(jsonStr);
  }

  @SneakyThrows
  public <T> T readAs(byte[] bytes, TypeReference<T> typeReference) {
    return objectMapper.readValue(bytes, typeReference);
  }
}
